package com.music.controller;

import com.github.pagehelper.PageInfo;
import com.music.pojo.Album;
import com.music.pojo.Playlist;
import com.music.pojo.UserInfo;
import com.music.service.AlbumService;
import com.music.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@Controller
@RequestMapping("album")
public class AlbumController {
    @Autowired
    private AlbumService albumService;

    @RequestMapping("search")
    @ResponseBody
    public PageInfo<Album> songPageInfo(String search, Integer pageNum) throws UnsupportedEncodingException {
        String _search = URLDecoder.decode(search,"UTF-8");
        return albumService.findAlbumBySearch(_search,pageNum);
    }

    @RequestMapping(value = "{alid}",method = RequestMethod.GET)
    @ResponseBody
    public ResultInfo albumDetail(@PathVariable Integer alid, HttpSession session){
        ResultInfo resultInfo = new ResultInfo(true);
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        Album album;
        if (userInfo==null){
            album = albumService.findAlbumDetail(alid,null);
        }else {
            album = albumService.findAlbumDetail(alid,userInfo.getUid());
        }
        resultInfo.setData(album);
        return resultInfo;
    }

    @RequestMapping("addFavoriteAlbum")
    @ResponseBody
    public ResultInfo addFavoriteAlbum(Integer alid,HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        if (userInfo==null){
            return new ResultInfo(false);
        }else {
            albumService.addFavoriteAlbum(alid,userInfo.getUid());
            return new ResultInfo(true);
        }
    }

    @RequestMapping("removeFavoriteAlbum")
    @ResponseBody
    public ResultInfo removeFavoriteAlbum(Integer alid,HttpSession session){
        ResultInfo resultInfo = new ResultInfo(true);
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        albumService.removeFavoriteAlbum(alid,userInfo.getUid());
        return resultInfo;
    }

    @RequestMapping("findCollectCount")
    @ResponseBody
    public ResultInfo findCollectCount(Integer alid){
        ResultInfo resultInfo = new ResultInfo(true);
        Integer num = albumService.findCollectCount(alid);
        resultInfo.setData(num);
        return resultInfo;
    }

    //查找关注页面的最近新专辑
    @RequestMapping("findUserFollowSingerAlbum")
    @ResponseBody
    public ResultInfo findUserFollowSingerAlbum(HttpSession session){
        UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
        ResultInfo resultInfo;
        if (userInfo==null){
            resultInfo = new ResultInfo(false);
        }else{
            resultInfo = new ResultInfo(true);
            List<Album> list = albumService.findUserFollowSingerAlbum(userInfo.getUid());
            resultInfo.setData(list);
        }
        return resultInfo;
    }
}
